package com.android.hedgehog;

import java.util.List;

import android.graphics.Color;

import com.android.hedgehog.BattleScreen.GameStatus;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Screen;
import com.android.hedgehog.framework.Input.TouchEvent;

public class BattleResultScreen extends Screen {
	enum GameState {
		Fail, Win, Finish
	}

	String testMsg = "";
	String testMsg2 = "";
	Graphics g;
	boolean win;
	int roundNum;
	int points;
	int turns;
	GameState state;
	float rawScore;
	int finalScore;
	int scoreStepper = 0;

	public BattleResultScreen(Game game, int roundNumber, boolean success, int playerScore, int totalTurns) {
		super(game);
		g = game.getGraphics();
		roundNum = roundNumber;
		points = playerScore;
		turns = totalTurns;
		win = success;
		if (win) {
			calculateScore();
			saveScore();
			state = GameState.Win;
		}
		if (!win) {
			state = GameState.Finish;
		}
		testMsg = " round: " + roundNumber + "  pScore: " + playerScore + " Win?: " + success + " turns: "
				+ totalTurns;
		testMsg2 = "rScore: " + rawScore + " fScore: " + finalScore + " cybScore: " + Settings.cyberScore
				+ " IQ: " + Settings.iqRating;
	}

	private void calculateScore() {// 120-200 points
		rawScore = 35 + (roundNum * 10);
		rawScore += (25 * ((double) points / (double) turns));
		finalScore = Math.round(rawScore);
	}

	private void saveScore() {
		Settings.addRoundScore(roundNum, finalScore);
		Settings.save(game.getFileIO());

	}

	@Override
	public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

		if (state == GameState.Finish)
			updateFinish(touchEvents);

	}

	private void updateFinish(List<TouchEvent> touchEvents) {

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {

				if (event.x < 80 && event.y > 260) {
					game.setScreen(new MainMenu2(game));
					if (Settings.soundEnabled)
						Assets.bitten.play(1);
					return;
				}
				if (event.x > 240 && event.y > 260) {
					if (win) {
						game.setScreen(new VersesScreen(game, roundNum + 1));
					} else {
						game.setScreen(new BattleScreen(game, roundNum));
					}

				}
			}
		}

	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.blackbackground, 0, 0);
		if (win) {
			g.drawSpecialText("SUCCESS!", 160, 90, 58, 2, Color.BLUE);
		} else {
			g.drawSpecialText("FAIL", 160, 90, 68, 2, Color.RED);
			// state = GameState.Finish;
		}

		if (state == GameState.Win) {
			scoreStepper++;
			if (scoreStepper < finalScore) {
				g.drawSpecialText("" + scoreStepper, 160, 290, 124, 2, Color.GREEN);
			} else {
				state = GameState.Finish;
			}

		}

		if (state == GameState.Finish) {
			g.drawRect(0, 420, 80, 60, Color.GRAY);
			g.drawRect(240, 420, 80, 60, Color.GRAY);
			g.drawSpecialText("Main Menu", 40, 460, 14, 1, Color.MAGENTA);
			g.drawRect2(0, 420, 80, 60, Color.MAGENTA);
			g.drawRect2(240, 420, 80, 60, Color.MAGENTA);
			if (win) {
				g.drawText("Next Round", 280, 460, 14, Color.MAGENTA);
				g.drawSpecialText("" + finalScore, 160, 310, 104, 2, Color.GREEN);
			} else
				g.drawText("Retry", 280, 460, 14, Color.MAGENTA);
		}

		g.drawText(testMsg, 160, 390, 12, Color.RED); // test
		g.drawText(testMsg2, 160, 410, 12, Color.RED); // test

	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
